package com.gs.core.domain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="PERSON")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="PERSON_TYPE", discriminatorType=DiscriminatorType.STRING)
public abstract class Person {
	private Long id;
	protected String firstName;
	protected String lastName;
	
	private List<Action> actions = new ArrayList<Action>();

	@Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="PERSON_ID", unique=true, nullable=false, scale=0)	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	@Column(name="FIRST_NAME")
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@Column(name="LAST_NAME")
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public void add(Action action){
		action.setActionPerformedBy(this);
		action.setDisplayOrder(Long.valueOf(this.actions.size()));
		action.setPerformedAt(new Date());
		this.actions.add(action);
	}
	
	@OneToMany(cascade=CascadeType.ALL,orphanRemoval=true)
    @JoinColumn(name="PERSON_ID")
	public List<Action> getActions() {
		return actions;
	}

	public void setActions(List<Action> actions) {
		this.actions = actions;
	}
}
